package com.qs.questionnairesurveysystem.controller;

import com.qs.questionnairesurveysystem.entity.User;
import com.qs.questionnairesurveysystem.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user")
@Api(tags = "User Controller")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "Get user by ID", notes = "Fetches a user by their ID.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "User retrieved successfully."),
            @ApiResponse(code = 404, message = "User not found.")
    })
    @GetMapping("/{userId}")
    public ResponseEntity<User> getUserById(@PathVariable Long userId) {
        User user = userService.getUserById(userId);
        if (user == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(user);
    }

    @ApiOperation(value = "Get all users", notes = "Fetches a list of all users.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Users retrieved successfully.")
    })
    @GetMapping("/")
    public ResponseEntity<List<User>> getAllUsers() {
        List<User> users = userService.getAllUsers();
        return ResponseEntity.ok(users);
    }

    @ApiOperation(value = "Create a new user", notes = "Creates a new user in the system.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "User created successfully."),
            @ApiResponse(code = 500, message = "Internal server error.")
    })
    @PostMapping("/")
    public ResponseEntity<User> createUser(@RequestBody User user) {
        int result = userService.createUser(user);
        if (result > 0) {
            return ResponseEntity.ok(user);
        }
        return ResponseEntity.status(500).build();
    }

    @ApiOperation(value = "Update user information", notes = "Updates the information of an existing user.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "User updated successfully."),
            @ApiResponse(code = 500, message = "Internal server error.")
    })
    @PutMapping("/{userId}")
    public ResponseEntity<User> updateUser(@PathVariable Long userId, @RequestBody User user) {
        user.setUserId(userId);
        int result = userService.updateUser(user);
        if (result > 0) {
            return ResponseEntity.ok(user);
        }
        return ResponseEntity.status(500).build();
    }

    @ApiOperation(value = "Delete a user", notes = "Deletes a user by their ID.")
    @ApiResponses(value = {
            @ApiResponse(code = 204, message = "User deleted successfully."),
            @ApiResponse(code = 500, message = "Internal server error.")
    })
    @DeleteMapping("/{userId}")
    public ResponseEntity<Void> deleteUser(@PathVariable Long userId) {
        int result = userService.deleteUser(userId);
        if (result > 0) {
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.status(500).build();
    }
}
